﻿@using System.Data
@using STX.Domain
@model STX.Admin.Models.BizActivity.EditCostModel
@{
    ViewBag.Title = "Add";
    Layout = null;
    STX.Domain.TabAdministrator CurUser = ViewBag.CurUser;
    var i = -1;
    string action = "saveauditfinance";
    var disabledQuote = " disabled=\"disabled\" readonly=\"readonly\"";
    var disabledCost = " disabled=\"disabled\" readonly=\"readonly\"";
    var disabledFinance = " disabled=\"disabled\" readonly=\"readonly\"";
    if (Model.Step == StepEnum.制作报价)
    {
        disabledQuote = "";
    }
    if (Model.Step == StepEnum.制作成本)
    {
        disabledCost = ""; 
    }
    if (Model.Step == StepEnum.财务经理制作成本)
    {
        disabledFinance = "";
        action = "saveeditfinance";
    }
}
<style>
    #editfinanceform .tempQPT{ position: absolute; width: 200px; z-index: 9; display: none;}
    #editfinanceform .tempQPT ul{ background: #fff;padding: 5px;border: #333333 1px solid;}
    #editfinanceform .tempQPT ul li{ border-bottom: #eee 1px solid;padding: 5px; cursor: pointer }
    #editfinanceform .tempQPT ul li:hover{ background:#f0e68c}
    #editfinanceform .tempQPT ul li.active{ background:#f0e68c}

    #editfinanceform .tempQuotePrice{ position: absolute; width: 200px; z-index: 9; display: none;}
    #editfinanceform .tempQuotePrice ul{ background: #fff;padding: 5px;border: #333333 1px solid;}
    #editfinanceform .tempQuotePrice ul li{ border-bottom: #eee 1px solid;padding: 5px; }
</style>

<form class="pageForm addpage" action="@Url.Action(action)" method="post" id="editfinanceform">
    <div style=" height:100%; width:100%; overflow:hidden" class="verticalSplitContainer">
        <div class="pageFormContent" ratio="*">
            <input type="hidden" value="@Model.Id" name="Id" />
            <input type="hidden" value="@Model.AreaId" name="AreaId" />
            <input type="hidden" value="@Model.ProjectId" name="ProjectId" />
            @if (!Model.Comment.IsNullOrEmpty())
            {
                <div class="leadercomment">领导意见：@Model.Comment</div>
            }
            <div class="inputActivityDiv">
                <table style="width:100%">
                    <tr>    
                        <td width="15%">活动名称：</td>
                        <td width="35%"><input name="Name" class="textInput" type="text" size="40" alt="" value="@Model.Name"@disabledQuote/></td>
                        <td width="15%">所属项目：</td>
                        <td width="35%">@Model.ProjectName
                        </td>
                    </tr>
                    <tr>    
                        <td width="15%">活动时间：</td>
                        <td width="35%"> <input name="StartTime" class="textInput datetime" type="text" size="8" value="@Model.StartTime.ToString("yyyy-M-d")" alt=""@disabledQuote/> - <input name="EndTime" class="textInput datetime" type="text" size="8" value="@Model.EndTime.ToString("yyyy-M-d")" alt=""@disabledQuote/> <input name="TimeDetail" class="textInput" type="text" size="16" value="@Model.TimeDetail"  alt=""@disabledQuote/></td>
                        <td width="15%">活动地点：</td>
                        <td width="35%"><input name="Place" class="textInput" type="text" size="30" value="@Model.Place" alt=""@disabledQuote/></td>
                    </tr>
                    <tr>    
                        <td width="15%">报价制作人：</td>
                        <td width="35%"><input name="QuotationTabulator" class="textInput" type="text" size="30" value="@Model.QuotationTabulator" readonly="readonly"  alt=""/></td>
                        <td width="15%">制表时间：</td>
                        <td width="35%"><input name="QuotationGeneratedTime" class="textInput datetime" type="text" size="15" value="@Model.QuotationGeneratedTime.ToString("yyyy-MM-dd")" disabled="disabled"  alt=""/></td>
                    </tr>
                    <tr>    
                        <td width="15%">成本制表人：</td>
                        <td width="35%"><input name="CostTabulator" class="textInput" type="text" size="30" value="@CurUser.Name"  alt=""@disabledQuote/></td>
                        <td width="15%">制表时间：</td>
                        <td width="35%"><input name="CostGeneratedTime" class="textInput datetime" type="text" size="15" value="@DateTime.Now.ToString("yyyy-MM-dd")" disabled="disabled"  alt=""/></td>
                    </tr>
                    <tr>
                        <td width="15%">执行：</td>
                        <td width="35%"><input name="Executor" class="textInput" type="text" size="30"  value="@Model.Executor" alt=""@disabledQuote/></td>    
                        <td width="15%">负责人：</td>
                        <td width="35%"><input name="ResponsiblePerson" class="textInput" type="text"  value="@Model.ResponsiblePerson" size="30"  alt=""@disabledQuote/></td>
                    </tr>
                    <tr>    
                        <td width="15%">策划：</td>
                        <td width="35%"><input name="Planner" class="textInput" type="text" size="30"  value="@Model.Planner" alt=""@disabledQuote/></td>
                        <td width="15%">司机：</td>
                        <td width="35%"><input name="Driver" class="textInput" type="text" size="30"  value="@Model.Driver" alt=""@disabledQuote/></td>
                    </tr>
                </table>
                <table style="width: 100%">
                        
                        <tr style="opacity: 0;">
                            <td style="width:100px;"><input name="" style="width:100px;" class="textInput" /></td>
                            <td style="width:200px;"><input name="" class="textInput" type="text" style="width:200px;"/> </td>
                            <td style="width:50px;"><input name="" class="textInput" type="text" style="width:50px;"/></td>
                            <td style="width:50px;"><input name="" class="textInput" type="text" style="width:50px;"/></td>
                            <td style="width:50px;"><input name="" class="textInput" type="text" style="width:50px;"/></td>
                            <td style="width:50px;"><input name="" class="textInput" type="text" style="width:50px;"/></td>
                            <td style="width:50px;"><input name="" class="textInput" type="text" style="width:50px;"/></td>
                            <td style="width:50px;"><input name="" class="textInput" type="text" style="width:50px;"/></td>
                            <td style="width:100px;"><input name="" class="textInput" type="text" style="width:100px;"/></td>
                            <td style="width:50px;"><input name="" class="textInput" type="text" style="width:50px;"/></td>
                            <td style="width:50px;"><input name="" class="textInput" type="text" style="width:50px;"/></td>
                            <td style="width:80px;">@(Html.DropDownListFromEnum<STX.Domain.BizActivityItem.MaterialFormEnum>(0, "select"))</td>
                            <td style="width:80px;">@(Html.DropDownListFromEnum<STX.Domain.BizActivityItem.MaterialBelongEnum>(0, "select"))</td>
                            <td style="width:80px;"><input type="checkbox">有问题</td>
                        </tr>
                        <tr>
                            <td style="width:100px;"><h6 style="width:90px;">项目</h6></td>
                            <td style="width:200px;"><h6 style="width:190px;">具体要求</h6></td>
                            <td style="width:50px;"><h6 style="width:50px;">单位</h6></td>
                            <td style="width:50px;"><h6 style="width:50px;">数量</h6></td>
                            <td style="width:50px;"><h6 style="width:50px;">报价</h6></td>
                            <td style="width:50px;"><h6 style="width:50px;">报价小计</h6></td>
                            <td style="width:50px;"><h6 style="width:50px;">项目成本</h6></td>
                            <td style="width:50px;"><h6 style="width:50px;">项目小计</h6></td>
                            <td style="width:100px;"><h6 style="width:100px;">供应商</h6></td>
                            <td style="width:50px;"><h6 style="width:50px;">单价</h6></td>
                            <td style="width:50px;"><h6 style="width:50px;">财务小计</h6></td>
                            <td style="width:80px;"><h6 style="width:50px;">物料形式</h6></td>
                            <td style="width:80px;"><h6 style="width:50px;">物料归属</h6></td>
                            <td style="width:80px;"><h6 style="width:80px;">状态</h6></td>
                        </tr>
                    </table>
                   @for (var j = 0; j < Model.MaterialCategories.Count; j++)
                   {
                       var item = Model.MaterialCategories[j];
                       var activityItems = Model.BizActivityItems.Where(o => o.MaterialCategoryId == item.Id);
                        <div class="materialitem">
                            <h4 style=" background: #c0c0c0; padding: 6px;">@item.Name</h4>
                            <table style="width: 100%" id="activitytb(@item.Id)">
                                @{
                                    var bizActivityItems = activityItems as BizActivityItem[] ?? activityItems.ToArray();
                                
                                    foreach (var activityItem in bizActivityItems)
                                    {
                                        i++;
                                        var curColor = "";
                                        var curReadonly = "";
                                        if (activityItem.ErrorStatus == BizActivityItem.ErrorStatusEnum.Error)
                                        {
                                            curColor = " style=background:#ff3300";
                                        }
                                        if (!activityItem.TpCode.IsNullOrEmpty())
                                        {
                                            curReadonly = " readonly=readonly";
                                        }
                                        <tr  class="datatr"@curColor>
                                            <td style="width:100px;" style="position: relative"><input onkeyup="getTemplateData(event,this)" name="BizActivityItems[@i].Name" value="@activityItem.Name" style="width:100px;" class="textInput" type="text"@disabledQuote/></td>
                                            <td style="width:200px;"><input name="BizActivityItems[@i].Require" class="textInput" type="text" style="width:200px;"  value="@activityItem.Require"@disabledQuote/> </td>
                                            <td style="width:50px;"><input name="BizActivityItems[@i].Unit" class="textInput" type="text" style="width:50px;"  value="@activityItem.Unit"@disabledQuote/></td>
                                            <td style="width:50px;" style="position: relative"><input name="BizActivityItems[@i].Numb" class="textInput" type="text" style="width:50px;"  onkeyup="calculate(this)" onfocus="showQuotePrice(this)"  onblur="hideQuotePrice(this)"  value="@activityItem.Numb"@disabledQuote/></td>
                                            <td style="width:50px;"><input name="BizActivityItems[@i].QuotationPrice" class="textInput" type="text" style="width:50px;" alt=""  value="@activityItem.QuotationPrice"@disabledQuote/></td>
                                            <td style="width:50px;"><input name="rowsumquote" class="textInput" disabled="disabled" type="text" style="width:50px;" value="0" alt=""/></td>
                                            <td style="width:50px;"><input name="BizActivityItems[@i].ProjectCostPrice" class="textInput" type="text" style="width:50px;" alt=""  value="@activityItem.ProjectCostPrice"@disabledCost/></td>
                                            <td style="width:50px;"><input name="rowsumcost" class="textInput" disabled="disabled" type="text" style="width:50px;" value="0" alt=""/></td>
                                            <td style="width:100px;">
                                                @{
                                                    var suppliers = Model.GetSuppliersByTpCode(activityItem.TpCode);
                                                    var issplit = "";
                                                    if (Model.IsNeedSplit(activityItem.TpCode))
                                                    {
                                                       issplit = " multiple=\"multiple\" size=\"5\" ";
                                                    }
                                                }
                                                @if (suppliers != null && suppliers.Count > 0)
                                                {  
                                                    <select@issplit  name="BizActivityItems[@i].SupplierIds"@disabledCost>
                                                        @foreach (var supplier in suppliers)
                                                        {
                                                            if (Model.CheckSupplierIsSelected(activityItem.Id, supplier.Id))
                                                            {
                                                                <option value="@supplier.Id">@supplier.Name</option>
                                                            }
                                                        }
                                                    </select>
                                                }
                                                
                                            </td>
                                            <td style="width:50px;"><input name="BizActivityItems[@i].FinanceCostPrice" class="textInput" type="text" style="width:50px;" alt="" onkeyup="SumAllTable()" onblur="SumAllTable()"  value="@activityItem.FinanceCostPrice"@disabledFinance/></td>
                                            <td style="width:50px;"><input name="rowsumfinance" class="textInput" disabled="disabled" type="text" style="width:50px;" value="0" alt=""/></td>
                                            <td style="width:80px;">
                                                @if (Model.Step == StepEnum.制作报价)
                                                {
                                                    @(Html.DropDownListFromEnum<STX.Domain.BizActivityItem.MaterialFormEnum>((int)activityItem.MaterialForm, "BizActivityItems[" + @i + "].MaterialForm"))
                                                }
                                                else
                                                {
                                                <select name="MaterialForm"@disabledQuote><option>@activityItem.MaterialForm.GetFiledName()</option></select>
                                                }
                                            </td>
                                            <td style="width:80px;">
                                                @if (Model.Step == StepEnum.制作报价)
                                                {
                                                    @(Html.DropDownListFromEnum<STX.Domain.BizActivityItem.MaterialBelongEnum>((int)activityItem.MaterialBelong, "BizActivityItems[" + @i + "].MaterialBelong"))
                                                }
                                                else
                                                {
                                                    <select name="MaterialForm"@disabledQuote><option>@activityItem.MaterialBelong.GetFiledName()</option></select>
                                                }
                                                
                                            </td>
                                            <td style="width:80px;">
                                                @if (Model.Step == StepEnum.财务经理制作成本
                                                     && activityItem.ErrorStatus == BizActivityItem.ErrorStatusEnum.Error)
                                                {
                                                    <text><input type="checkbox" name="BizActivityItems[@i].ErrorStatus" value="@((int)BizActivityItem.ErrorStatusEnum.None)" />已修复</text>
                                                }
                                                else if (Model.Step != StepEnum.财务经理制作成本 && activityItem.ErrorStatus != BizActivityItem.ErrorStatusEnum.Error)
                                                {
                                                    <text><input type="checkbox" name="BizActivityItems[@i].ErrorStatus" value="@((int)BizActivityItem.ErrorStatusEnum.Error)" />有问题</text>
                                                }<input type="hidden" name="BizActivityItems[@i].TpCode" value="@activityItem.TpCode"/><input type="hidden" name="BizActivityItems[@i].MaterialCategoryId" value="@item.Id"/><input type="hidden" name="BizActivityItems[@i].Id" value="@activityItem.Id"/>
                                            </td>
                                        </tr>
                                    }
                                    if (bizActivityItems.Any())
                                    {
                                        <tr>
                                            <td colspan="5" align="left">总计</td>
                                            <td align="left"><input name="sumquotebycat" class="textInput" disabled="disabled" type="text" style="width:50px;" value="0" alt=""/></td>
                                            <td></td>
                                            <td align="left"><input name="sumcostbycat" class="textInput" disabled="disabled" type="text" style="width:50px;" value="0" alt=""/></td>
                                            <td colspan="2"></td>
                                            <td  colspan="3" align="left"><input name="sumfinancebycat" class="textInput" disabled="disabled" type="text" style="width:50px;" value="0" alt=""/></td>
                                        </tr>
                                    }
                                }

                            </table>
                            
                        </div>
                   }
                   <table style="width: 100%">
                        
                        <tr>
                            <td>整张表合计 报价：<input name="totalquote" disabled="disabled" class="textInput" type="text" style="width:100px;"/> 
                            项目成本合计：<input name="totalcost" disabled="disabled" class="textInput" type="text" style="width:100px;"/>
                            财务成本合计：<input name="totalfinance" disabled="disabled" class="textInput" type="text" style="width:100px;"/>
                            </td>
                        </tr>
                       
                    </table>
                </div>
        </div>

        <div class="formBar" ratio="#30">
            <ul>
                <li><div class="simplebutton"><div class="r"><button type="button" class="submit">提交</button></div></div></li>
                @Html.Partial("~/Administrator/Views/BizActivity/Step.cshtml", new STX.Admin.Models.BizActivity.StepModel(Model.Step))
            </ul>
        </div>
    </div>
    <script type="text/javascript">
        function SumTable($table, fun) {
            $table.find("input[name='sumquotebycat']").val(0);
            $table.find("input[name='sumcostbycat']").val(0);
            $table.find("input[name='sumfinancebycat']").val(0);
            $("tr.datatr", $table).each(function () {
                var numb = parseFloat($(this).find("input[name*='Numb']").val());
                var quoteprice = parseFloat($(this).find("input[name*='QuotationPrice']").val());
                var costprice = parseFloat($(this).find("input[name*='ProjectCostPrice']").val());
                var financeprice = parseFloat($(this).find("input[name*='FinanceCostPrice']").val());
                var sumquotebycat = parseFloat($table.find("input[name='sumquotebycat']").val());
                var sumcostbycat = parseFloat($table.find("input[name='sumcostbycat']").val());
                var sumfinancebycat = parseFloat($table.find("input[name='sumfinancebycat']").val());
                var rowsum = numb * quoteprice;
                var rowsumcost = numb * costprice;
                var rowsumfinance = numb * financeprice;

                $(this).find("input[name='rowsumquote']").val(rowsum.toFixed(2));
                $(this).find("input[name='rowsumcost']").val(rowsumcost.toFixed(2));
                $(this).find("input[name='rowsumfinance']").val(rowsumfinance.toFixed(2));

                $table.find("input[name='sumquotebycat']").val((sumquotebycat + rowsum).toFixed(2));
                $table.find("input[name='sumcostbycat']").val((sumcostbycat + rowsumcost).toFixed(2));
                $table.find("input[name='sumfinancebycat']").val((sumfinancebycat + rowsumfinance).toFixed(2));

                fun(rowsum, rowsumcost, rowsumfinance);
            });
        }
        function SumAllTable() {
            $("input[name='totalquote']").val(0);
            $("input[name='totalcost']").val(0);
            $("input[name='totalfinance']").val(0);
            $("#editfinanceform .materialitem table").each(function () {
                SumTable($(this), function (cur_tb_total_quote, cur_tb_total_cost, cur_tb_total_finance) {
                    var totalquote = parseFloat($("input[name='totalquote']").val());
                    var totalcost = parseFloat($("input[name='totalcost']").val());
                    var totalfinance = parseFloat($("input[name='totalfinance']").val());
                    $("input[name='totalquote']").val((totalquote + cur_tb_total_quote).toFixed(2));
                    $("input[name='totalcost']").val((totalcost + cur_tb_total_cost).toFixed(2));
                    $("input[name='totalfinance']").val((totalfinance + cur_tb_total_finance).toFixed(2));
                });
            });
        }

        $(function () {

            $("#editfinanceform input").disabledinput();

            $("#editfinanceform .materialitem tr").each(function () {
                var $cost = $(this).find($("input[name*='ProjectCostPrice']"));
                if (parseInt($cost.val()) > 0) {
                    $cost.attr("readonly", "readonly");
                    $cost.disabledinput();
                    return;
                }
                var $td = $cost.parent();
                var $suppliertd = $td.next();
                var $select = $suppliertd.find("select[name*='SupplierId']");
                if ($select.size() > 0) {
                    $cost.val($select.find('option:first').attr("cost"));
                    $cost.attr("readonly", "readonly");
                    $cost.disabledinput();
                } else {
                    $cost.val($td.prev().find("input").val());
                }
            });

            $("#editfinanceform  select[name*='SupplierId']").change(function () {
                $(this).parent().prev().find("input").val($(this).find("option:selected").attr("cost"));
            });

            SumAllTable();
        });
    </script>
</form>